extends Spatial
const Item_spot = preload("res://src/item_spot.gd")

var speed = 2.0
onready var body = $body
onready var raycast = $body/RayCast

signal open_placement_ui
signal open_seat_ui
# Called when the node enters the scene tree for the first time.
func _ready():
	$"../map".connect("build", self, "on_map_build")
	
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(_delta):
	if G.in_ui: return
	var movement = Vector3(0,0,0)
	if Input.is_action_pressed("move_up"):
		movement.z -= speed
#		raycast.rotation_degrees.y = 180
	if Input.is_action_pressed("move_down"):
		movement.z += speed
#		raycast.rotation_degrees.y = 0
	if Input.is_action_pressed("move_left"):
		movement.x -= speed
#		raycast.rotation_degrees.y = -90
	if Input.is_action_pressed("move_right"):
		movement.x += speed
	if movement.x != 0 or movement.z != 0:
		var rot = Vector2(0.0,0.0).angle_to_point(Vector2(movement.x, -movement.z))+(-PI/2)
		raycast.rotation.y = rot
#		raycast.rotation_degrees.y = 90
	body.move_and_slide(movement, Vector3(0,1,0))
	
	
	if Input.is_action_just_pressed("ui_accept"):
		var collider = raycast.get_collider()
		if collider and collider.get_parent() is Item_spot:
			emit_signal("open_placement_ui", collider.get_parent())
	

func on_map_build():
	self.transform.origin = $"../map/".player_start_position

func portrait(emote=""):
	return preload("res://2d/player.png")
